<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :xs="20" :sm="18" :md="19" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <div>{{ $route.query.username }}的所有下级</div>
          <div style="margin-top: 15px;">
            <!-- 搜索 -->
            <el-input
              v-model="query.blurry"
              clearable
              size="small"
              placeholder="输入名称或者邮箱搜索"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
            <date-range-picker v-model="query.createTime" class="date-item" unlink-panels />
            <el-select
              v-model="query.enabled"
              clearable
              size="small"
              placeholder="状态"
              class="filter-item"
              style="width: 90px"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in enabledTypeOptions"
                :key="item.key"
                :label="item.display_name"
                :value="item.key"
              />
            </el-select>
            <el-select
              v-if="dict && dict.user_type"
              v-model="query.userType"
              clearable
              size="small"
              placeholder="用户类型"
              class="filter-item"
              style="width: 100px"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.user_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-select
              v-if="dict && dict.user_level"
              v-model="query.userLevel"
              clearable
              size="small"
              placeholder="会员等级"
              class="filter-item"
              style="width: 100px"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.user_level"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <rrOperation />
          </div>
          <crudOperation :permission="permission" />
        </div>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;">
          <el-table-column :show-overflow-tooltip="true" prop="username" label="用户名" />
          <el-table-column :show-overflow-tooltip="true" prop="nickName" label="昵称" />
          <el-table-column width="50" prop="id" label="ID">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column width="100" prop="id" label="备注">
            <template slot-scope="scope">
              <span>{{ scope.row.email }}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户类型" align="center" prop="userType">
            <template slot-scope="scope">
              <span>{{ (dict.user_type.find(v=>(v.value) === `${scope.row.userType || 0}`) || {}).label }}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户等级" align="center" prop="userLevel">
            <template slot-scope="scope">
              <span>{{ (dict.user_level.find(v=>(v.value) === `${scope.row.userLevel || 0}`)|| {}).label }}</span>
            </template>
          </el-table-column>
          <!--          <el-table-column prop="gender" label="性别" />-->
          <!--          <el-table-column :show-overflow-tooltip="true" prop="phone" width="100" label="电话" />-->
          <!--          <el-table-column :show-overflow-tooltip="true" width="135" prop="email" label="邮箱" />-->
          <el-table-column label="状态" align="center" prop="enabled">
            <template slot-scope="scope">
              <span>{{ scope.row.enabled ? '激活' : '禁用' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="parentUser" align="center" label="上级">
            <template slot-scope="scope">
              <div>{{ scope.row.parentUser ? scope.row.parentUser.username: '--' }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="childrenUserCount" label="下级数量">
            <template slot-scope="scope">
              <a @click="getChildren(scope.row.id, scope.row.username)">{{ scope.row.childrenUserCount }}</a>
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期" />
        </el-table>
        <!--分页组件-->
        <pagination />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudUser from '@/api/system/user'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { mapGetters } from 'vuex'
import crudOperation from '@crud/CRUD.operation'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
const defaultForm = { id: null, username: null, nickName: null, vipTime: [], gender: '男', userType: '0', userLevel: '0', email: null, enabled: 'true', roles: [], jobs: [], dept: { id: null }, phone: null }
export default {
  name: 'User',
  components: { rrOperation, pagination, DateRangePicker, crudOperation },
  cruds() {
    return CRUD({ title: '用户', url: 'api/users', queryOnPresenterCreated: false, crudMethod: { ...crudUser }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['user_status', 'user_type', 'user_level'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      deptName: '', depts: [], deptDatas: [], jobs: [], level: 3, roles: [],
      jobDatas: [], roleDatas: [], // 多选时使用
      defaultProps: { children: 'children', label: 'name', isLeaf: 'leaf' },
      enabledTypeOptions: [
        { key: 'true', display_name: '激活' },
        { key: 'false', display_name: '锁定' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  created() {
    this.crud.optShow = {
      add: false,
      edit: false,
      del: false,
      download: true
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
    this.crud.query.pid = this.$route.params.id
    this.crud.query.appLoginTimeNotNull = true
    this.crud.query.enabled = true
    this.crud.toQuery()
  },
  methods: {
    getChildren(id, username) {
      this.$router.push({
        path: '/user/children/' + id,
        query: { username }
      })
    },
    // 禁止输入空格
    keydown(e) {
      if (e.keyCode === 32) {
        e.returnValue = false
      }
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
